#define  _CRT_SECURE_NO_WARNINGS 1
#include "queue.h"

void QueueInit(Queue* pq)
{
	assert(pq);

	pq->head = NULL;
	pq->tail = NULL;
}

void QueueDestory(Queue* pq)
{
	assert(pq);

	QNode* curr = pq->head;
	while (curr)
	{
		QNode* del = curr;
		curr = curr->next;
		free(del);
	}
	pq->head = NULL;
	pq->tail = NULL;
}

QNode* BuyQNode(QNodeDataType x)
{
	QNode* pn = (QNode*)malloc(sizeof(QNode));
	if (!pn)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	pn->data = x;
	pn->next = NULL;
	return pn;
}

void QueuePush(Queue* pq, QNodeDataType x)
{
	assert(pq);

	QNode* pn = BuyQNode(x);
	if (pq->head == NULL)
	{
		pq->head = pq->tail = pn;
	}
	else
	{
		pq->tail->next = pn;
		pq->tail = pn;
	}
}

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->head);

	if (pq->head->next == NULL)
	{
		free(pq->tail);
		pq->tail = pq->head = NULL;
	}
	else
	{
		QNode* del = pq->head;
		pq->head = pq->head->next;
		free(del);
	}
}

QNodeDataType QueueDront(Queue* pq)
{
	assert(pq);
	assert(pq->head);

	return pq->head->data;
}

QNodeDataType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->tail);

	return pq->tail->data;
}

int QueueSize(Queue* pq)
{
	assert(pq);
	QNode* curr = pq->head;
	int size = 0;
	while (curr)
	{
		size++;
		curr = curr->next;
	}
	return size;
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->head == NULL;
}